<?php
/**
 * Created by PhpStorm.
 * User: xufei
 * Date: 2018/5/7
 * Time: 下午5:36
 */

class wxapi_ctl_orders extends wxapi_response
{
    public $pagedata;

    public function __construct(&$app)
    {
        parent::__construct($app);
    }

    /**
     * 订单取消
     * 
     */
    function docancel()
    {
        $member = $this->get_current_member();
        if(empty($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
                'status' => 0
            ];
            return self::return_json($return);
        }
        
        //开启事务处理
        $db = kernel::database();
        $transaction_status = $db->beginTransaction();

        $order_cancel_reason = $_POST;

        if($order_cancel_reason['reason_type'] == 7 && !$order_cancel_reason['reason_desc'])
        {
            $return = [
                'result' => '请输入详细原因',
                'code' =>  5001,
                'status' => 0
            ];
            return self::return_json($return);
        }
        if(strlen($order_cancel_reason['reason_desc'])>150)
        {
            $return = [
                'result' => '详细原因过长，请输入50个字以内',
                'code' =>  5001,
                'status' => 0
            ];
            return self::return_json($return);
        }
        if($order_cancel_reason['reason_type'] != 7 && strlen($order_cancel_reason['reason_desc']) > 0)
        {
            $order_cancel_reason['reason_desc'] = '';
        }
        $order_cancel_reason = utils::_filter_input($order_cancel_reason);
        $order_cancel_reason['cancel_time'] = time();
        $mdl_order = app::get('b2c')->model('orders');
        $sdf_order_member_id = $mdl_order->getRow('member_id', array('order_id'=>$order_cancel_reason['order_id']));
        if($sdf_order_member_id['member_id'] != $member['member_id'])
        {
            $db->rollback();
            $return = [
                'result' => '请勿取消别人的订单',
                'code' =>  5001,
                'status' => 0
            ];
            return self::return_json($return);
        }

        $order_payed = kernel::single('b2c_order_pay')->check_payed($order_cancel_reason['order_id']);
        if($order_payed>0){
            $return = [
                'result' => '支付过的订单，无法取消订单',
                'code' =>  5001,
                'status' => 0
            ];
            return self::return_json($return);
        }

        $mdl_order_cancel_reason = app::get('b2c')->model('order_cancel_reason');
        $result = $mdl_order_cancel_reason->save($order_cancel_reason);
        if(!$result)
        {
            $db->rollback();
            $return = [
                'result' => '订单取消原因记录失败',
                'code' =>  5001,
                'status' => 0
            ];
            return self::return_json($return);
        }
        $obj_checkorder = kernel::service('b2c_order_apps', array('content_path'=>'b2c_order_checkorder'));
        if (!$obj_checkorder->check_order_cancel($order_cancel_reason['order_id'],'',$message))
        {
            $db->rollback();
            $return = [
                'result' => $message,
                'code' =>  5001,
                'status' => 0
            ];
            return self::return_json($return);
        }

        $sdf['order_id'] = $order_cancel_reason['order_id'];
        $sdf['op_id'] = $member['member_id'];
        $sdf['opname'] = '';
        $sdf['account_type'] = 'member';

        $b2c_order_cancel = kernel::single("b2c_order_cancel");
        if ($b2c_order_cancel->generate($sdf))
        {
            if($order_object = kernel::service('b2c_order_rpc_async')){
                $order_object->modifyActive($sdf['order_id']);
            }
            $db->commit($transaction_status);
            $obj_coupon = kernel::single("b2c_coupon_order");
            $obj_coupon->use_c($sdf['order_id'],'cancel');
            $return = [
                'result' => '订单取消成功',
                'code' =>  0,
                'status' => 1
            ];
            return self::return_json($return);
        }
        else
        {
            $db->rollback();
            $return = [
                'result' => '订单取消失败',
                'code' =>  5001,
                'status' => 0
            ];
            return self::return_json($return);
        }
    }

    public function toCancel(Request $request)
    {
        $orderNo = trim($_GET['orderNo']);
        if(!$orderNo ){
            $return = [
                'result' => '无订单号参数',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        $member = $this->get_current_member();
        if(empty($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        //开启事务处理
        $db = kernel::database();
        $transaction_status = $db->beginTransaction();

        $order_cancel_reason['cancel_time'] = time();
        $mdl_order = app::get('b2c')->model('orders');
        $sdf_order_member_id = $mdl_order->getRow('member_id', array('order_id'=>$orderNo));
        if($sdf_order_member_id['member_id'] != $member['member_id'])
        {
            $db->rollback();
            $return = [
                'result' => '请勿取消别人的订单',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        $order_payed = kernel::single('b2c_order_pay')->check_payed($order_cancel_reason['order_id']);
        if($order_payed>0){
            $return = [
                'result' => '支付过的订单，无法取消订单',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        $mdl_order_cancel_reason = app::get('b2c')->model('order_cancel_reason');
        $order_cancel_reason['order_id'] = $orderNo;
        $order_cancel_reason['reason_type'] = 0;
        $result = $mdl_order_cancel_reason->save($order_cancel_reason);
        if(!$result)
        {
            $db->rollback();

            $return = [
                'result' => '订单取消原因记录失败',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        $sdf['order_id'] = $orderNo;
        $sdf['op_id'] = $member['member_id'];
        $sdf['opname'] = '';
        $sdf['account_type'] = 'member';

        $b2c_order_cancel = kernel::single("b2c_order_cancel");
        if ($b2c_order_cancel->generate($sdf))
        {
            if($order_object = kernel::service('b2c_order_rpc_async')){
                $order_object->modifyActive($sdf['order_id']);
            }
            $db->commit($transaction_status);
            $return = array('code'=>0,'msg'=>'订单取消成功');
            return self::return_json($return);
        }
        else
        {
            $db->rollback();
            $return = [
                'result' => '取消失败',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
    }

    public function toConfirm(){
        $order_id = trim($_GET['orderNo']);
        if(!$order_id ){
            $return = [
                'result' => '无订单号参数',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        $member = $this->get_current_member();
        if(empty($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        $mdl_order = app::get('b2c')->model('orders');
        $sdf_order_member_id = $mdl_order->getRow('member_id', array('order_id'=>$order_id));
        $sdf_order_member_id['member_id'] = (int) $sdf_order_member_id['member_id'];
        if($sdf_order_member_id['member_id'] != $member['member_id'])
        {
            $return = [
                'result' => '请勿操作别人的收货',
                'code' => 5001,
            ];
            return self::return_json($return);
        }else{
            $arr_updates = array('order_id'=>$order_id,'received_status' =>'1','received_time'=>time());
            $mdl_order->save($arr_updates);
            $delivery_mdl = app::get('b2c')->model('order_delivery_time');
            $delivery_mdl->delete(array('order_id' => $order_id));
            $orderLog = app::get('b2c')->model("order_log");
            $sdf_order_log = array(
                'rel_id' => $order_id,
                'op_id' => $member['member_id'],
                'op_name' => (!$member['member_id']) ? app::get('b2c')->_('顾客') : $member['uname'],
                'alttime' => time(),
                'bill_type' => 'order',
                'behavior' => 'receive',
                'result' => 'SUCCESS',
                'log_text' => '用户已确认收货！',
            );
            if($orderLog->save($sdf_order_log)){
                $return = array('code'=>0,'msg'=>'已完成收货');
                return self::return_json($return);
            }else{
                $return = [
                    'result' => '收货失败',
                    'code' => 5001,
                ];
                return self::return_json($return);
            }
        }
    }


    private function get_current_member()
    {
        return self::user_auth();
    }
}